package com.storydo.file.share.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**   
* @Title: DateUtils.java 
* @Description: TODO
* @author : Storydo
* @date: 2018年11月22日 上午11:39:03 
* @version:V1.0 
* Copyright 悦享互联 2016 All right reserved.
* Modification  History:
* Version       Date          Author          Description
* ----------------------------------------------------------------------------
*  1.0         2018年11月22日        Storydo              TODO
*/

public class DateUtils {

	public static final String YYYY_MM_DD_HH_MM_SS_S = "yyyy-MM-dd HH:mm:ss.S";
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String YYYY_MM = "yyyy-MM";
	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static final String YYYY = "yyyy";
	public static final String MM = "MM";
	public static final String DD = "dd";

	public static final String YYYY_MM_DD_CN = "yyyy年MM月dd";
	public static final String YYYY_MM_DD_HH_MM_SS_CN = "yyyy年MM月dd日  HH时mm分ss秒";
	public static final String YYYY_MM_DD_HH_MM_SS_S_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";

	private static ZoneOffset defaultZoneOffset = null;

    public static Date format(String strDate) {
        return format(strDate, getDefaultPattern());
    }

    public static Date format(String strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String format(Date date) {
        return format(date, getDefaultPattern());
    }

    public static String format(Date date, String pattern) {
        String returnValue = "";
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }
        return (returnValue);
    }

    public static String getDefaultPattern() {
        return YYYY_MM_DD_HH_MM_SS;
    }
	
/** @since jdk1.8  */
    
    public static String formatDateTime(LocalDateTime dateTime) {
    	return formatDateTime(dateTime, getDefaultPattern());
    }
    
    public static LocalDateTime formatDateTime(String dateTime) {
    	return formatDateTime(dateTime, getDefaultPattern());
    }
    
    public static String formatDateTime(LocalDateTime dateTime, String pattern) {
    	DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
    	return dateTime.format(formatter);
    }
    
    public static LocalDateTime formatDateTime(String dateTime,String pattern) {
    	DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
    	return LocalDateTime.parse(dateTime, formatter);
    }
    
    public static ZoneOffset getDefaultZoneOffset() {
    	if(defaultZoneOffset == null) {
    		defaultZoneOffset = ZonedDateTime.now().getOffset();
    	}
    	return defaultZoneOffset;
    }

}
